<?php
namespace app\admin\business;
use \app\common\model\mysql\AdminUser as AdminUserModel;
use think\Exception;

class AdminUser
{
    public static function login($data){
        try {
            //调用模型查询该用户数据
            $adminUserObj = new AdminUserModel;
            $adminUser = $adminUserObj->getAdminUserByUsername($data['username']);
            if (empty($adminUser) || $adminUser->status != config('status.mysql.table_normal')) {
                throw new Exception('不存在该用户');
//                return show(config('status.error'), '不存在该用户!');
            }

            $adminUser = $adminUser->toArray();

            //判断密码是否与输入的一致
            $tabPass = $adminUser['password'];
            if ($tabPass !== md5($data['password'] . '_singwa_abc')) {
                throw new Exception('用户名或者密码错误');

//                return show(config('status.error'), '用户名或者密码错误!');
            }

            //需要记录信息到mysql中
            $updateData = [
                'last_login_time' => time(),
                'last_login_ip' => request()->ip(),
                'update_time' => time()
            ];
            $res = $adminUserObj->updateById($adminUser['id'], $updateData);
            if (empty($res)) {
                throw new Exception('登录失败');

//                return show(config('status.error'), '登录失败!');

            }
        } catch (\Exception $e) {
            // todo 记录日志 $e->getMessage();
            throw new Exception('内部异常,登录失败!');

//            return show(config('status.error'), '内部异常,登录失败!');

        }

        //把登录信息加入session
        session(config('admin.session_admin'), $adminUser);

        return true;

    }
}